package com.jzy.gulimall.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzy.common.to.MemberPrice;
import com.jzy.common.to.SkuReductionTo;
import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.Query;
import com.jzy.gulimall.coupon.dao.SkuFullReductionDao;
import com.jzy.gulimall.coupon.entity.MemberPriceEntity;
import com.jzy.gulimall.coupon.entity.SkuFullReductionEntity;
import com.jzy.gulimall.coupon.entity.SkuLadderEntity;
import com.jzy.gulimall.coupon.service.MemberPriceService;
import com.jzy.gulimall.coupon.service.SkuFullReductionService;
import com.jzy.gulimall.coupon.service.SkuLadderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;

    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 发布商品 保存 满减打折会员
     */
    @Override
    public void saveReduction(SkuReductionTo skuReductionTo) {
        //1.sms_sku_ladder  打折
        SkuLadderEntity ladderEntity = new SkuLadderEntity();
        ladderEntity.setSkuId(skuReductionTo.getSkuId());
        ladderEntity.setFullCount(skuReductionTo.getFullCount());
        ladderEntity.setDiscount(skuReductionTo.getDiscount());
        ladderEntity.setAddOther(skuReductionTo.getCountStatus());
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(ladderEntity);
        }
        //2.sms_sku_full_reduction 满减
        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
        BeanUtil.copyProperties(skuReductionTo, fullReductionEntity);
        fullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());
        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
            this.save(fullReductionEntity);
        }

        //3.sms_member_price 会员
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream()
                .filter(item -> item.getPrice().compareTo(new BigDecimal(0)) > 0)
                .map(item -> {
            MemberPriceEntity priceEntity = new MemberPriceEntity();
            priceEntity.setSkuId(skuReductionTo.getSkuId());
            priceEntity.setMemberLevelId(item.getId());
            priceEntity.setMemberLevelName(item.getName());
            priceEntity.setMemberPrice(item.getPrice());
            priceEntity.setAddOther(1);
            return priceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}